import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class code1 {
    //辅助方法，判断字符串是否符合yyyy/MM/dd的格式  借鉴网络地址：https://blog.csdn.net/dangerousroy/article/details/46124655
    public static boolean isValidDate(String sDate) {
        String datePattern1 = "\\d{4}/\\d{2}/\\d{2}";
        String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
                + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
                + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
                + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        if ((sDate != null)) {
            Pattern pattern = Pattern.compile(datePattern1);
            Matcher match = pattern.matcher(sDate);
            if (((Matcher) match).matches()) {
                pattern = Pattern.compile(datePattern2);
                match = pattern.matcher(sDate);
                return match.matches();
            } else {
                return false;
            }
        }
        return false;
    }

    //编程题1：给出年月日信息，计算得到这一年的第几天
    //解题思路，通过现有api和包很容易获取
    public static void GetDay() {
        System.out.println("请输入日期如下格式 yyyy/MM/dd");
        Scanner sc = new Scanner(System.in);

        String dateStr = sc.nextLine();
        if (!isValidDate(dateStr)) {
            System.out.println("请确定日期是否为如下格式 yyyy/MM/dd");
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        java.util.Date startDate = null;
        try {
            startDate = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        int index = startCalendar.get(Calendar.DAY_OF_YEAR);
        System.out.println("这是" + startCalendar.get(Calendar.YEAR) + "年" + ":" + "第" + index + "天");

    }

    //编程题2：输出1000内的完数
    //先获取该数，再从该数中获取因子，通过因子相加是否等于本身判断是否为完数
    public static void PerfectNumber() {
        System.out.println("编程题2：输出1000内的完数");
        for (int iInputNumber = 2; iInputNumber <= 1000; iInputNumber++) {
            int iOutNumber = 0;
            for (int i = 1; i < iInputNumber; i++) {
                //通过求余数是否为零得到因子
                if (iInputNumber % i == 0) {
                    iOutNumber += i;
                }
            }
            if (iOutNumber == iInputNumber) {
                System.out.println("输出完数：" + iInputNumber);
            }
        }
    }

    //辅助方法：获取1到n的随机数  输入信息  最大数  总个数
    public static void GetRandomNumber(String strType, int iMaxNumber, int iCount) {
        Random ran = new Random();
        HashSet hs = new HashSet();
        for (; ; ) {
            int iTmp = ran.nextInt(iMaxNumber) + 1;
            hs.add(iTmp);
            if (hs.size() == iCount) break;
        }
        System.out.println(strType + hs);

    }

    //编程题3：双色球 在java中可以通过HashSet自动过滤重复元素
    public static void DoubelColorBall() {
        GetRandomNumber("红球号码为", 31, 6);
        GetRandomNumber("蓝球号码为", 16, 1);
    }

    //编程题4：动态扩容
    public static void DynamicGrowthArray() {
        int[] iArray = {1, 2, 3, 4, 5, 0, 0, 0, 0, 0};
        System.out.println("初始数组大小为10，存有5个元素，请输入要增加的整型元素");
        Scanner scan = new Scanner(System.in);
        int iCount = 5;
        while (true) {
            int iNum = scan.nextInt();
            if ((double) iCount / iArray.length >= 0.8) {
                int newLen = (int) (1.5 * iArray.length);
                iArray = Arrays.copyOf(iArray, newLen);
                System.out.println("扩容后的数组大小为："+ iArray.length);
            }
            iArray[iCount] = iNum;
            iCount ++;
        }
    }

    //题五 绘制棋盘
    public static void PaintBoard() {
        int iBoardSize = 15;
        /*用二维数组定义棋盘*/
        char iElements[][] = new char[iBoardSize][iBoardSize];
        for (int i = 0; i < iBoardSize; i++) {//初始化
            for (int j = 0; j < iBoardSize; j++) {
                iElements[i][j] = '+';
            }
        }
        //打印标尺 列数
        char cChar[] = new char[]{'a','b','c','d','e','f'};
        System.out.println("   1 2 3 4 5 6 7 8 9 a b c d e f");
        for (int i = 0; i < iBoardSize; i++) {
            if (i < 9) {
                System.out.print(i+1);
            }else{
                System.out.print(cChar[i-9]);
            }
            System.out.print(" ");
            //打印标尺 行数
            for (int j = 0; j < iBoardSize; j++) {
                System.out.print(" " + iElements[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        //题1 给出年月日信息，计算得到这一年的第几天
        GetDay();
        //题2 输出1000内的完数
        PerfectNumber();
        //题3 双色球
        DoubelColorBall();
        //题4 动态扩容
        DynamicGrowthArray();
        //题5 绘制棋盘
        PaintBoard();
    }

}
